/**
 * 库存store.js
 * 版本号： v1.0.0
 */
$(function () {
    
    /**
     * 库存列表
     */
    var lastChecked = 0;
    $("#store_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        striped: true,
        pageSize: 10,
        pageNumber: 1,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/store/storeList.do',
        idField: 'coId',
        frozenColumns: [[
            {field: 'coId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'project', title: '项目名称', width: 200, fixed: true,
                    formatter:function(value){
                        if(value){
                            return value.prName;
                        }
                    }},
                {field: 'contract', title: '合同编号', width: 160, fixed: true,
                    formatter:function(value){
                        if(value){
                            return value.coNumber;
                        }
                    }},
                {
                    field: 'prLeader_', title: '项目负责人', width: 80, fixed: true,
                    formatter: function (value,row) {
                        if (row.project && row.project.prLeader) {
                            return row.project.prLeader;
                        }
                    }
                },
                {
                    field: 'prRealStart_', title: '项目开工日期', width: 100,
                    formatter: function (value,row) {
                        if (row.project && row.project.prRealStart) {
                            return row.project.prRealStart;
                        }
                    }
                }
            ]
        ],
        onCheck: function (rowIndex, rowData) {
            var row = $("#store_dataList").datagrid("getChecked");
            if (row && rowIndex == lastChecked) {
                $('#store_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            } else {
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#store_dataList").datagrid("selectRow", 0);
            }
        }
    });


    //定义可操作按钮
    $("#store_menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });


});


/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            store_loadSearch();
                        }
                    });
                }
                //入库按钮
                if ("putBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            storeIn();
                        }
                    });
                }
                //出库按钮
                if ("pullBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            storeOut();
                        }
                    });
                }
                //查看详情按钮
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            store_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 *  加载查询
 */
function store_loadSearch() {
    //刷新dg页码
    refreshPageNumber("store_dataList");
    //查询数据
    $("#store_dataList").datagrid("reload", {
        prName: $("#prName").textbox("getValue"),
        coNumber: $("#coNumber").textbox("getValue")
    });
}


/**
 * 查看详情
 */
function store_view(iconCls) {
    var row = $("#store_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({title: '操作提示', msg: "请选择合同信息", timeout: 3000, showType: "slide"});
        return;
    }
    var row = $("#store_dataList").datagrid("getSelected");
    //IFRAM打开窗口
    layoutSub(kcxq.id, kcxq.url + row.coId, iconCls, kcxq.closable, kcxq.title);
}

/**
 * 跳转出库
 */
function storeOut(){
    var row = $("#store_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({title: '操作提示', msg: "请选择合同信息", timeout: 3000, showType: "slide"});
        return;
    }
    var row = $("#store_dataList").datagrid("getSelected");
    window.location.href = CONTEXT_PATH + "/store/toPullStore.do?coId=" + row.coId;
}

/**
 * 跳转入库
 */
function storeIn(){
    var row = $("#store_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({title: '操作提示', msg: "请选择合同信息", timeout: 3000, showType: "slide"});
        return;
    }
    var row = $("#store_dataList").datagrid("getSelected");
    window.location.href = CONTEXT_PATH + "/store/toPutStore.do?coId=" + row.coId;
}














